<?php

/**
 * Created by PhpStorm.
 * User: dapeng
 * Date: 2018/9/29
 * Time: 下午3:10
 */
include_once "MyPDO.php";
include_once "Action.php";
class Student extends Action
{

    private $pagenum = 5;//每页显示10条记录

    //作用:当调用对象不存在的方法时,会进入该方法

    //学生的列表
    //访问方式: index.php?action=student&func=lists&page=1
    function lists(){

        //获取页码
        $page = isset($_GET["page"]) ? $_GET["page"] : 1;

        //计算limit的值
        $cc = ($page - 1) * $this->pagenum;

        $db = MyPDO::getInstance();
        $sql = "select * from t_student ORDER BY create_time DESC  limit $cc,$this->pagenum ";
        //分页查询到学生的记录
        $students = $db->select($sql);

        $sql2 = "select count(*) from t_student";
        $count_page = $db->select($sql2);
        //总页数 = 总记录数 / 每页数量
        $count_page = ceil($count_page[0]['count(*)'] / $this->pagenum);

        $this->tp->assign("students",$students);
        $this->tp->assign("page",$page);
        $this->tp->assign("count_page",$count_page);
        //学生对象的List
        $this->tp->display("student/list.html");

    }

    //访问方式: index.php?action=student&func=add
    function add(){

        $stuid = !empty($_POST['stuid'])?$_POST['stuid']:die("请填写学号");
        $name = $_POST['name'];
        $sex = $_POST['sex'];
        $date = $_POST['date'];
        $mobile = $_POST['mobile'];

        $db =  MyPDO::getInstance();
        $sql = "insert into t_student(`Stud_id`,`Stud_name`,`Stud_sex`,`Birth_date`,`Mobile`) VALUES ('$stuid','$name','$sex','$date','$mobile');";
        $res = $db->insert($sql);

        if($res > 0){
            //跳转页面
            echo "<meta http-equiv=\"refresh\" content=\"1; url=index.php?action=student&func=lists&page=1\">";
            print('添加成功,正在拼命的跳转页面...<br>1秒后自动跳转~~~');
        }else{
            echo "添加失败";
        }
    }

    //3、保存
    //访问方式: index.php?action=student&func=edit
    function edit(){

        $stuid = $_POST['stuid'];
        $name = $_POST['name'];
        $sex = $_POST['Stud_sex'];
        $date = $_POST['date'];
        $mobile = $_POST['mobile'];

        $db = MyPDO::getInstance();

        $sql = "update t_student set Stud_id='$stuid',Stud_name='$name',Stud_sex='$sex',`Birth_date`='$date' ,`Mobile`='$mobile' where Stud_id = '$stuid';";

        $res = $db->insert($sql);
        if($res > 0){
            //跳转页面
            echo "<meta http-equiv=\"refresh\" content=\"1; url=index.php?action=student&func=lists&page=1\">";
            print('修改成功,正在拼命的跳转页面...<br>1秒后自动跳转~~~');
        }else{
            echo "修改失败";
        }
    }





    //就为显示模板页面
    function add_html(){
        $this->tp->display("student/add.html");
    }
    // //1、先查询 就为显示模板页面
    function edit_html(){
        //查询数据
        $id = $_GET['id'];

        $db = MyPDO::getInstance();
        $sql = "select * from t_student where stud_id=$id";
        $students = $db->select($sql);
        //有数据
        if(count($students) > 0){
            $student = $students[0];

            $this->tp->assign("student",$student);
            $this->tp->display("student/edit.html");
        }

    }


    //index.php?action=student&func=del&id=xxx
    function del(){

       if (!isset($_GET["id"])){
           $id = die("大胖子框架Error: 缺少必要参数id");
       }else{
           $id = $_GET["id"];
       }
       $sql  = "delete from t_student where stud_id=$id";
        $db = MyPDO::getInstance();
        $res = $db->delete($sql);
        if ($res){
            echo "删除成功";
            echo "<meta http-equiv=\"refresh\" content=\"1; url=index.php?action=student&func=lists&page=1\">";

        }else{
            echo "删除失败";
        }

    }

    //当调用对象不存在的方法时,会自动进入该方法
    function __call($name, $arguments)
    {
        die("大胖子框架Error:温馨提醒不存在{$name}方法");
//        echo "进入到call方法";
    }


}